package com.wangxl.rabbitmqdemo.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * spring application context util
 * @Author: wangxl
 * @Date: 2019/3/23 14:20
 */
@Component
@Slf4j
public class ApplicationContextUtil implements ApplicationContextAware {

    private static ApplicationContext context;


    public static ApplicationContext getContext() {
        return context;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

        this.context = applicationContext;
        if(this.context != null){
            log.info("ApplicationContextUtil init success");
        }else{
            log.info("ApplicationContextUtil init fail");
        }
    }

    public final static Object getBean(String beanName){
        return context.getBean(beanName);
    }

    public final static Object getBean(String beanName, Class<?> requiredType) {
        return context.getBean(beanName, requiredType);
    }

}
